﻿using System;
using System.Web;
using System.Web.UI;

namespace Elmah
{
    internal sealed class CqVipLogin : ErrorPageBase
    {
        private const string LocalUserName = "cqvip";
        private const string LocalPassword = "com.cqvip.www";
        private string _errorMsg;
        private string _returnPath;
        protected override void OnLoad(EventArgs e)
        {
            string text = base.Request["postflag"];
            if (!string.IsNullOrEmpty(text) && text == "1")
            {
                this.ValidateUser();
            }
            this._returnPath = base.Request["returnpath"];
            this.PageTitle = "登陆页面";
            base.OnLoad(e);
        }
        private void ValidateUser()
        {
            string text = base.Request["username"];
            string text2 = base.Request["password"];
            this._returnPath = base.Request["returnpath"];
            if (!text.Equals(LocalUserName, StringComparison.OrdinalIgnoreCase) || !text2.Equals(LocalPassword, StringComparison.OrdinalIgnoreCase))
            {
                this._errorMsg = "用户名或密码不正确";
                return;
            }
            HttpCookie httpCookie = new HttpCookie("cqvip", "true");
            httpCookie.Expires = DateTime.Now.AddDays(1.0);
            base.Response.Cookies.Add(httpCookie);
            if (!string.IsNullOrEmpty(this._returnPath))
            {
                HttpContext.Current.Response.Redirect(this._returnPath);
                return;
            }
            HttpContext.Current.Response.Redirect("/elmah.axd");
        }
        protected override void RenderHead(HtmlTextWriter writer)
        {
            //
            // In IE 8 or later, mimic IE 7
            // http://msdn.microsoft.com/en-us/library/cc288325.aspx#DCModes
            //

            writer.AddAttribute("http-equiv", "X-UA-Compatible");
            writer.AddAttribute("content", "IE=EmulateIE7");
            writer.RenderBeginTag(HtmlTextWriterTag.Meta);
            writer.RenderEndTag();
            writer.WriteLine();

            //
            // Write the document title.
            //

            writer.RenderBeginTag(HtmlTextWriterTag.Title);
            Server.HtmlEncode(this.PageTitle, writer);
            writer.RenderEndTag();
            writer.WriteLine();
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.Write(string.Format("<form action='{0}' method='post' name='form1' id='form1'>", base.BasePageName + "?returnpath=" + this._returnPath));
            writer.Write("<table align='center' >");
            writer.Write("<tr><td align='left' style='width:240px; height: 50px;' valign='middle'><label>用户名</label><input type='text' name='username' size='20'/></td></tr>");
            writer.Write("<tr><td align='left' style='width:240px; height: 50px;' valign='middle'><label>密码</label><input type='password' name='password' size='20'/></td></tr>");
            writer.Write("<tr><td align='left' style='width:240px; height: 50px;' valign='middle'><input type='submit' name='submit' value='登陆'/></td></tr>");
            if (!string.IsNullOrEmpty(this._errorMsg))
            {
                writer.Write(string.Format("<tr><td align='left' style='width:240px; height: 50px;' valign='middle'>{0}</td></tr>", this._errorMsg));
            }
            writer.Write("</table>");
            writer.Write("<input type='hidden' name='postflag' value='1'/>");
            writer.Write("</form>");
        }
    }
}
